﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Message.aspx.cs" Inherits="UI_Mail_Message" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title>Message</title>
    <link href="../../css/admin.css" rel="stylesheet" type="text/css" />
    <link href="../../css/datagrid.css" rel="stylesheet" type="text/css" />
    <link href="../../js/jquery.easyui/themes/metro/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../js/jquery.easyui/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../js/jquery.easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <style type="text/css">
      .m-btn-downarrow {
        display: none;
      }
    </style>
    <script type="text/javascript">
      var request = {
        QueryString: function (val) {
          var url = window.location.search;
          var re = new RegExp('' + val + '=([^&?]*)', 'ig');
          return ((url.match(re)) ? (url.match(re)[0].substr(val.length + 1)) : null);
        }
      };
      var eBayUserID = request.QueryString('ebayuserid');
      var treeID = request.QueryString('treeid');
      var idx = undefined;
      var setRowIdx = function (index) {
        idx = index;
      };

     var markMsgRead = function (userID, msgID, read) {
       if (read == 'False') {
         $.getJSON(
           './MailHandler.ashx?act=u&id=0107&ebayuserid=' + userID + '&MessageID=' + msgID,
           function (data) {
             if (data.retVal == 0) {
               
             }
           });
       }
     };

      $(function () {
       $('#searchtype').combobox({
         valueField: 'id',
         textField: 'text',
         data: [
           { id: 'OrderSaleRecordNumber', text: '订单号' },
           { id: 'CaseID', text: 'Case ID' },
           { id: 'CustomerID', text: '客户ID' },
           { id: 'CustomerName', text: '客户名称' }
         ],
         value: 'OrderSaleRecordNumber',
         panelHeight: 'auto'
       });

       $('#category').combobox({
         formatter: function (row) {
           var imageFile = '../../Images/1.gif';
           return '<img class="item-img" src="' + imageFile + '"/><span class="item-text">' + row.text + '</span>';
         }
       });

       $('#update').window({
         width: 300,
         height: 160,
         title: '订单修改',
         modal: true,
         shadow: false,
         closed: true,
         collapsible: false,
         minimizable: false,
         maximizable: false,
         closable: false
       });

       $('#btnupdateok').click(function () {
         var msgID = $('#txtmsgid').val();
         var orderno = $('#txtorderno').val();
         $.getJSON(
           './MailHandler.ashx?act=u&id=0108&ebayuserid=' + eBayUserID + '&messageid=' + msgID + '&ordersalesrecordernumber=' + orderno,
           function (data) {
             if (data.retVal == 0) {
               $('#txtmsgid').val('');
               $('#txtorderno').val('');
               $('#update').window('close');
               $('#tbmessage').datagrid('reload');
             }
             else {
               alert('修改失败, 原因: ' + data.retMsg);
             }
           });
       });

       $('#btnupdatecancel').click(function () {
         $('#txtmsgid').val('');
         $('#txtorderno').val('');
         $('#update').window('close');
       });

       var editIndex = undefined;
       $('#tbmessage').datagrid({
         url: '../../TableHanders.aspx?act=q&tid=8016&type=0',
         loadMsg: '正在加载, 请稍后...',
         pagination: true,
         fitColumns: true,
         sortName: 'ReceiveDate',
         sortOrder: 'asc',
         checkOnSelect: false,
         columns: [[
           { field: 'ck', checkbox: true },
           {
             field: 'Flag', width: 60, fixed: true,
             formatter: function (value, row, index) {
               return '<a id="mb' + index + '" href="javascript:void(0)" class="easyui-menubutton" data-options="menu:\'#menuFlag\'" onmouseover="setRowIdx(' + index + ')"><img src="../../'+row['Url']+'" width="20" height="20" /></a>';
             }
           },
           {
             field: 'File', width: 30, fixed: true,
             formatter: function (value, row, index) {
               var imgPath = value ? 'file.png' : 'unfile.png';
               return '<img src="../../Images/' + imgPath + '" width="16" height="16" />';
             }
           },
           {
             field: 'Read', width: 30, fixed: true,
             formatter: function (value, row, index) { var image = value ? 'read.png' : 'unread.png'; return '<img src="../../Images/' + image + '" width="20" height="20" />'; }
           },
           { field: 'Sender', title: '发件人', width: 80, fixed: true, editor: { type: 'text' } },
           {
             field: 'Subject', title: '标题', width: 250,
             formatter: function (value, row, index) {
               var link = row.OrderSalesRecordNumber ?
                 '<a href="./MailMenu.aspx?ebayuserid=' + eBayUserID + '&orderid=' + row.OrderID + '&page=message" target="_blank" onclick="markMsgRead(\'' + eBayUserID + '\',' + row.MessageID + ',\'' + row.Read + '\');">' + value + '</a>' :
                 '<a href="./MessageDetail.aspx?ebayuserid=' + eBayUserID + '&messageid=' + row.MessageID + '" target="_blank" onclick="markMsgRead(\'' + eBayUserID + '\',' + row.MessageID + ',\'' + row.Read + '\');">' + value + '</a>';

               return $('<div/>').append($(link).css('font-weight', 'bold')).html();
             }
           },
           {
             field: 'OrderSalesRecordNumber', title: '订单号', width: 80, fixed: true,
             formatter: function (value, row, index) {
               if (row.OrderSalesRecordNumber) {
                 return '<a href="#" onclick="openUpdate(' + row.MessageID + ',' + row.OrderSalesRecordNumber + ');">' + value + '</a>';
               }
               return '<a href="#" onclick="openUpdate(' + row.MessageID + ');">修改</a>';
             }
           },
           { field: 'ReceiveDate', title: '发件日期', width: 120, fixed: true, sortable: true },
           {
             field: 'Operation', title: '动作', width: 60, fixed: true,
             formatter: function(value, row, index) {
               return '<a id="mbop' + index + '" href="javascript:void(0)" class="easyui-menubutton" data-options="menu:\'#mm2\'" onmouseover="setRowIdx(' + index + ')">动作</a>';
             }
           }
         ]],
         pageList: [10, 20, 50],
         onLoadSuccess: function (data) {
           for (i = 0; i < $('#tbmessage').datagrid('getRows').length; i++) {
             $('#mb' + i).menubutton();
             $('#mbop' + i).menubutton();
           }
         },
         onDblClickRow: function (rowIndex, rowData) {
           if (editIndex == rowIndex) return;
           if (editIndex != undefined) {
             $('#tbmessage').datagrid('endEdit', editIndex);
           }
           $('#tbmessage').datagrid('beginEdit', rowIndex);
           editIndex = rowIndex;
         },
         queryParams: { 'eBayUserID': eBayUserID, 'TreeID': treeID }
       });

       $('#btnsearch').click(function() {
         var isAdvSearch = $('#hdIsAdvSearch').val();
         if (isAdvSearch) {
           var keyword = $('#txtKeyword').val();
           var product = $('#txtProduct').val();
           var startdate = $('#txtStartDate').val();
           var enddate = $('#txtEndDate').val();

           if (keyword == '' && product == '' && startdate == '' && enddate == '') {
             alert('选项不能全为空');
           }
           else {
             $('#tbmessage').datagrid({
               url: '../../TableHanders.aspx?act=q&tid=8024&type=0',
               queryParams: { 'ebayuserid': eBayUserID, 'keyword': keyword, 'product': product, 'startdate': startdate, 'enddate': enddate }
             });
           }
         }
         else {
           var item = $('#searchtype').combobox('getValue');
           var text = $('#searchtext').val();
           var show = $('#showhistory').prop('checked');
           
           $('#tbmessage').datagrid({
             url: '../../TableHanders.aspx?act=q&tid=8019&type=0',
             queryParams: { 'ebayuserid': eBayUserID, 'item': item, 'text': text, 'show': show }
           });
         }
       });

       $('#btnfile').click(function () {
         var rows = $('#tbmessage').datagrid('getSelections');
         for (i in rows) {
           $.getJSON(
             './MailHandler.ashx?act=u&id=0103&messageid=' + rows[i].MessageID,
             function (data) {
               if (data.retVal == 0) {
                 alert('归档成功');
               }
               else {
                 alert(data.retMsg);
               }
             }
           );
         }
       });
       
       $('#btnresponse').click(function () {
         var rows = $('#tbmessage').datagrid('getChecked');
         if (rows.length == 0) {
           alert('选中行数为空');
         }
         else {
           var row = rows[0];
           markMsgRead(eBayUserID, row.MessageID, row.read);
           window.open('./MessageDetail.aspx?ebayuserid=' + eBayUserID + '&orderid=' + row.OrderID);
         }
       });

       // Button 标记站外信 click event
       $('#btnoutside').click(function() {
         $.getJSON(
           "./MailHandler.ashx?act=q&id=0011",
           function(data) {
           });
       });

       $('#menuFlagHeader').menu({
         onClick: function (item) {
           var rows = $('#tbmessage').datagrid('getChecked');
           var msgIDs = [];
           for(i in rows) {
             msgIDs.push(rows[i].MessageID);
           }

           $.post(
             './MailHandler.ashx?act=u&id=0111&ts=' + new Date().getTime(),
             { eBayUserID: eBayUserID, MessageIDs: JSON.stringify(msgIDs), Name: item.name },
              function (data) {
                var result = $.parseJSON(data);
                if (result.retVal == 0) {
                  $('#tbmessage').datagrid('reload');
                }
                else {
                  alert(result.retMsg);
                }
              });
         }
       });

       $('#menuFlag').menu({
           onClick: function (item) {
               var rows = $('#tbmessage').datagrid('getRows');
               if (rows[idx]) {
                   $.post(
                     './MailHandler.ashx?act=u&id=0111&ts=' + new Date().getTime(),
                     { eBayUserID: eBayUserID, MessageIDs: '[\"' + rows[idx].MessageID + '\"]', Name: item.name },
                     function (data) {
                         var result = $.parseJSON(data);
                         if (result.retVal == 0) {
                             $('#tbmessage').datagrid('reload');
                         }
                         else {
                             alert(result.retMsg);
                         }
                     });
               }
           }
       });
       
       $('#mm2').menu({
         onClick: function (item) {
           var rows = $('#tbmessage').datagrid('getRows');
           if (rows[idx]) {
             switch (item.id) {
               case "mmManuallySelector":
               case "mmFromeBay":
               case "mmPreSale":
               case "mmDT":
               case "mmIAD":
               case "mmPP":
               case "mmCOM":
               case "mmOther":
               case "mmHighPriority":
               case "mmAccountThreat":
               case "mmFromManager":
               case "mmeBaySupport":
                 {
                   $.getJSON(
                     './MailHandler.ashx?act=u&id=0105&ebayuserid=' + eBayUserID + '&messageid=' + rows[idx].MessageID + '&treeid=' + item.name,
                     function (data) {
                       if (data.retVal == 0) {
                         alert('移动成功');
                         $('#tbmessage').datagrid('reload');
                       }
                     });
                 }
                 break;
               case "mm2-file":
                 {
                   $.getJSON(
                     './MailHandler.ashx?act=u&id=0103&ebayuserid=' + eBayUserID + '&messageid=' + rows[idx].MessageID,
                     function (data) {
                       if (data.retVal == 0) {
                         alert('归档成功');
                       }
                       else {
                         alert(data.retMsg);
                       }
                   });
                 }
                 break;
               case "mm2-response":
                 {
                   window.open('./MessageDetail.aspx?ebayuserid=' + eBayUserID + '&orderid=' + rows[idx].OrderID);
                 }
                 break;
               case "mm2-upgrade":
                 {
                   $.getJSON(
                     './MailHandler.ashx?act=u&id=0104&ebayuserid=' + eBayUserID + '&messageid=' + rows[idx].MessageID,
                     function (data) {
                       if (data.retVal == 0) {
                         alert('升级成功');
                         $('#tbmessage').datagrid('reload');
                       }
                       else {
                         alert(data.retMsg);
                       }
                   });
                 }
                 break;
               case "mm2-forward":
                 {
                   alert('暂未实现');
                 }
                 break;
               case "mm2-delete":
                 {
                   $.getJSON(
                     './MailHandler.ashx?act=d&id=0102&ebayuserid=' + eBayUserID + '&messageid=' + row[idx].MessageID,
                     function (data) {
                       if (data.retVal == 0) {
                         alert('删除成功');
                       }
                       else {
                         alert(data.retMsg);
                       }
                     }
                     );
                 }
                 break;

             }
           }
         }
       });
         
       $('#mm3').menu({
         onClick: function (item) {
           var treeID = item.name;
           var rows = $('#tbmessage').datagrid('getChecked');
           var msg = '';
           for (i in rows) {
             $.getJSON(
               './MailHandler.ashx?act=u&id=0105&ebayuserid=' + eBayUserID + '&messageid=' + rows[i].MessageID + '&treeid=' + treeID,
               function (data) {
                 if (data.retVal != 0) {
                   msg += 'Message ID: ' + rows[i].MessageID + '\n';
                 }
               });
           }
           if (msg) alert(msg + '移动失败...');
         }
       });
     });

     var openUpdate = function (msgID, orderNo) {
       $('#txtmsgid').val(msgID);
       $('#txtorderno').val(orderNo);
       $('#update').window('open');
     };

     var moreSearchItem = function(sender) {
       $('#moreSearch').toggle();
       if($('#moreSearch').css('display') == 'none') {
         $('#hdIsAdvSearch').val(0);
         $(sender).text('显示高级搜索');
       }
       else {
         $('#hdIsAdvSearch').val(1);
         $(sender).text('隐藏高级搜索');
       }
     };
    </script>
  </head>
  <body>
    <div style="padding:5px 10px;">
      <input id="searchtype" name="searchtype" style="width:80px;" />
      <input id="searchtext" type="text" />
      <a id="btnsearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'"></a>
      <a id="advsearch" href="#" onclick="moreSearchItem(this)">显示高级搜索</a>
      <label><input id="showhistory" name="showhistory" type="checkbox" value="" />显示60天前历史</label>
    </div>
    <div id="moreSearch" style="margin:5px 20px;padding:5px 5px;display:none;background-color:#EEEEEE;">
      <span>关键字:</span><input id="txtKeyword" type="text" />
      <span>产品代码:</span><input id="txtProduct" type="text" />
      <span>起始时间:</span><input id="txtStartDate" type="text" class="easyui-datebox" />
      <span>结束时间:</span><input id="txtEndDate" type="text" class="easyui-datebox" />
    </div>
    <div style="padding:5px 10px;">
      <a id="btnfile" class="easyui-linkbutton">归档</a>
      <a id="btnresponse" class="easyui-linkbutton">回复</a>
      <a id="mbtnflag" href="javascript:void(0)" class="easyui-menubutton" data-options="menu:'#menuFlagHeader'"><img src="../../Images/flag0.png" /></a>
      <a id="btnmove" href="javascript:void(0)" class="easyui-menubutton" data-options="menu:'#mm3'" >移动</a>
      <a id="btnoutside" class="easyui-linkbutton">标记站外信</a>
    </div>
    <div>
      <table id="tbmessage" data-options="nowrap: false,cls: 'lines-bottom'" style="height:auto;">
      </table>
    </div>
    <div id="menuFlagHeader" style="width:60px;" runat="server">
    </div>
    <div id="menuFlag" style="width:60px;" runat="server">
    </div>
    <div id="mm2" style="width:75px;">
      <div id="mm2-file">归档</div>
      <div id="mm2-response">回复</div>
      <div id="mm2-upgrade">升级</div>
      <div id="mm2-forward">转发</div>
      <div id="mm2-delete">删除</div>
      <div>
        <span>移动</span>
        <div style="width:75px;">
          <div id="mmManuallySelector" data-options="name:'7'">Manually Selector</div>
          <div id="mmAuto" data-options="name:'8'">
            <span>Auto</span>
            <div style="width:75px;">
              <div id="mmFromeBay" data-options="name:'9'">From eBay</div>
              <div id="mmFromMember" data-options="name:'10'">
                <span>From Member</span>
                <div style="width:75px;">
                  <div id="mmPreSale" data-options="name:'11'">售前</div>
                  <div id="mmDT" data-options="name:'12'">DT</div>
                  <div id="mmIAD" data-options="name:'13'">IAD</div>
                  <div id="mmPP" data-options="name:'14'">P&P</div>
                  <div id="mmCOM" data-options="name:'15'">COM</div>
                  <div id="mmOther" data-options="name:'16'">Other</div>
                </div>
              </div>
              <div id="mmHighPriority" data-options="name:'17'">
                <span>High Priority</span>
                <div style="width:75px">
                  <div id="mmAccountThreat" data-options="name:'18'">Account Threat</div>
                  <div id="mmFromManager" data-options="name:'19'">From Manager</div>
                  <div id="mmeBaySupport" data-options="name:'20'">eBay Support</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div id="mm3" style="width:75px;">
      <div data-options="name:'7'">Manually Selector</div>
      <div data-options="name:'8'">
        <span>Auto</span>
        <div style="width:75px;">
          <div data-options="name:'9'">From eBay</div>
          <div data-options="name:'10'">
            <span>From Member</span>
            <div style="width:75px;">
              <div data-options="name:'11'">售前</div>
              <div data-options="name:'12'">DT</div>
              <div data-options="name:'13'">IAD</div>
              <div data-options="name:'14'">P&P</div>
              <div data-options="name:'15'">COM</div>
              <div data-options="name:'16'">Other</div>
            </div>
          </div>
          <div data-options="name:'17'">
            <span>High Priority</span>
            <div style="width:75px">
              <div data-options="name:'18'">Account Threat</div>
              <div data-options="name:'19'">From Manager</div>
              <div data-options="name:'20'">eBay Support</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div id="update">
      <div style="height:70px;line-height:30px;">
        <table id="tbupdate">
          <tr>
            <td><span>消息ID:</span></td>
            <td><input type="text" id="txtmsgid" style="width:210px" readonly="true"/></td>
          </tr>
          <tr>
            <td><span>订单ID:</span></td>
            <td><input type="text" id="txtorderno" style="width:210px"/></td>
          </tr>
        </table>
      </div>
      <div style="text-align:right;height:20px;line-height:20px; padding:12px;">
        <a id="btnupdateok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">确认</a>
        <a id="btnupdatecancel" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">取消</a>
      </div>
    </div>
    <input id="hdIsAdvSearch" type="hidden" name="hdIsAdvSearch" />
  </body>
</html>
